﻿Imports Negocio
Imports Servicios
Imports Excepciones

Public Class frmABMActividad
    Private _tarea As New Tarea()
    Private _actividad As New Actividad()

    Protected Overrides Sub PreLoad()
        Me.Icon = Icon.FromHandle(Global.SIPRO.My.Resources.Resources.activity.GetHicon())
    End Sub

    Sub New(tarea As Tarea)
        InitializeComponent()
        _tarea = tarea
    End Sub
    Sub New(actividad As Actividad, tarea As Tarea)
        Me.New(tarea)
        _actividad = actividad
        btnGuardar.Tag = "Modificar"
    End Sub

    Private Sub frmABMTarea_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Init()
        txtDescripcion.DataBindings.Add("Text", _actividad, "Descripcion")
        dtFecha.Value = _actividad.Fecha
        numProgreso.DataBindings.Add("Value", _actividad, "Progreso")
        cbTipo.DataBindings.Add("SelectedItem", _actividad, "Tipo")
    End Sub

    Private Sub Init()

        dtFecha.Value = DateTime.Now
        Try
            cbTipo.DataSource = GesTipoActividad.Consultar()
            numProgreso.Maximum = GesActividades.ProgresoRestante(_actividad, _tarea)
        Catch ex As Exception
            My.Application.HandlerException(ex)
            Me.Close()
        End Try

    End Sub
    Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click
        If MyBase.ValidateForm() Then
            Try
                _tarea.FechaInicio = dtFecha.Value
                GesActividades.Guardar(_actividad, _tarea)
                Me.Close()
                Me.DialogResult = Windows.Forms.DialogResult.OK
            Catch ex As Exception
                My.Application.HandlerException(New TareaSaveException())
            End Try
        End If
    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btnArchivos_Click(sender As System.Object, e As System.EventArgs) Handles btnArchivos.Click

    End Sub

    Private Sub cbTipo_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbTipo.SelectedIndexChanged
        Try
            If TypeOf cbTipo.SelectedItem Is TipoActividad Then
                Dim tipo As New TipoActividad
                tipo = DirectCast(cbTipo.SelectedItem, TipoActividad)
                If tipo.Progreso Then
                    numProgreso.Enabled = True
                Else
                    numProgreso.Value = 0
                    _actividad.Progreso = 0
                    numProgreso.Enabled = False
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class